"use strict"
var linUI = {
	swiper : function(e){
		function funSetEleStyle(){
			let ele = document.querySelector(e.ele);
			let eleStyle = {
				width : e.width + "px",
				overflow : "hidden",
				position : "relative",
				height : e.height + "px",
			}
			for(let eleStyleKey in eleStyle){
				ele.style[eleStyleKey] = eleStyle[eleStyleKey];
			}
			let createWrap = funCreateWrap();
			let wrapDom = ele.appendChild(createWrap);
			for(let imgKey in e.data){
				let createImg = funCreateImg(imgKey);
				let ImgDom = wrapDom.appendChild(createImg);
			}
			let createImg = funCreateImg(0);
			let ImgDom = wrapDom.appendChild(createImg);
			
			funRollImg(wrapDom);
		}
		function funCreateWrap(){
			let wrap = document.createElement('div');
			wrap.className = 'transition1';
			let wrapStyle = {
				position : "absolute",
				transform : "translateX(0%)",
				left : "0",
				top : "0",
				width : 100 * (e.data.length + 1) + "%",
				height : e.height + "px",
				background : "#ccc",
			}
			for(let wrapStyleKey in wrapStyle){
				wrap.style[wrapStyleKey] = wrapStyle[wrapStyleKey];
			}
			return wrap;
		}
		function funCreateImg(imgKey){
			let img = document.createElement('a');
			let imgStyle = {
				background : `url(${e.data[imgKey].src})`,
				'background-size' : e.data[imgKey].imgSize,
				'background-position' : "center",
				'background-repeat' : "no-repeat",
				width : 100 / (e.data.length + 1) + "%",
				height : e.height + "px",
				float : "left",
				display:"block",
			}
			for(let imgStyleKey in imgStyle){
				img.style[imgStyleKey] = imgStyle[imgStyleKey];
				img.href = e.data[imgKey].href;
			}
			return img;
		}
		function funRollImg(wrapDom){
			let nowImgIndex,nowLeftNum;
			if(e.rollDirection == "right"){					
				nowImgIndex = e.data.length;
				nowLeftNum = -e.data.length * e.width;
			}else{
				nowImgIndex = 1;
				nowLeftNum = 0;
			}
			function funSetTimer(){
				let timer,needRollTo;
				if(e.rollDirection == "right"){
					timer = setInterval(function(){
						needRollTo = -(nowImgIndex - 1) * e.width;
						nowLeftNum = nowLeftNum + 1;
						if(nowLeftNum >= needRollTo){
							if(needRollTo >= 0){
								nowImgIndex = e.data.length;
								nowLeftNum = -e.data.length * e.width;
								clearInterval(timer);
								setTimeout(function(){
									funSetTimer();
								},e.speed)
							}else{							
								nowImgIndex -= 1;
								clearInterval(timer);
								setTimeout(function(){
									funSetTimer();
								},e.speed)
							}
						}
						wrapDom.style.left = nowLeftNum + "px";
					}, 1);
				}else{
					timer = setInterval(function(){
						needRollTo = -nowImgIndex * e.width;
						nowLeftNum = nowLeftNum - 1;
						if(nowLeftNum <= needRollTo){
							if(needRollTo <= -(e.data.length) * e.width){
								nowImgIndex = 1;
								nowLeftNum = 0;
								clearInterval(timer);
								setTimeout(function(){
									funSetTimer();
								},e.speed)
							}else{							
								nowImgIndex += 1;
								clearInterval(timer);
								setTimeout(function(){
									funSetTimer();
								},e.speed)
							}
						}
						wrapDom.style.left = nowLeftNum + "px";
					}, 1);
				}
			}
			funSetTimer();
		}
		funSetEleStyle();
	}
};